home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / dev / gui / MPGui5xs.lha / sg / RunMPGui.c < prev    next >
C/C++ Source or Header  |  1997-02-26  |  9KB  |  367 lines

  1. // MPGui - requester library
  2. // Copyright (C) © 1995 Mark John Paddock
  3.  
  4. // mark@topic.demon.co.uk
  5. // mpaddock@cix.compulink.co.uk
  6.  
  7. // This source is freely distributable
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <proto/intuition.h>
  12. #include <proto/amigaguide.h>
  13. #include <proto/locale.h>
  14. extern struct Library *AmigaGuideBase = NULL;
  15. extern struct Library *LocaleBase = NULL;
  16.  
  17. #include <dos/dos.h>
  18.  
  19. #include <libraries/MPGui.h>
  20. #include <pragmas/MPGui_pragmas.h>
  21. #include <clib/MPGui_protos.h>
  22. #include <dos.h>
  23. #include <string.h>
  24. #include <dos/rdargs.h>
  25. #include <dos/dostags.h>
  26.  
  27. #define TEMPLATE "FROM/A,TO/K,RELMOUSE/S,PUBSCREEN/K,HELP/K,CHELP/S,NEWLINE/S,PREFS/S,BUTTONS/K,NOBUTTONS/S,PARAMS/K/M"
  28.  
  29. #define OPT_FILE            0
  30. #define OPT_TO                1
  31. #define OPT_MOUSE            2
  32. #define OPT_SCREEN        3
  33. #define OPT_HELP            4
  34. #define OPT_CHELP            5
  35. #define OPT_NEWLINE        6
  36. #define OPT_PREFS            7
  37. #define OPT_BUTTONS        8
  38. #define OPT_NOBUTTONS    9
  39. #define OPT_PARAMS        10
  40.  
  41. #define OPT_COUNT            11
  42.  
  43. extern long __oslibversion=39;
  44.  
  45. struct Library *MPGuiBase;
  46.  
  47. const char Version[]="$VER: RunMPGui 5.4 (26.2.97)";
  48.  
  49. AMIGAGUIDECONTEXT     handle     = NULL;
  50. struct NewAmigaGuide    nag         = {NULL};
  51. struct AmigaGuideMsg *agm;            // message from amigaguide
  52. ULONG ASig = 0;
  53.  
  54. int sprintf(char *buffer,char *ctl, ...);
  55.  
  56. extern long __stack = 16000;
  57.  
  58. extern char *ButtonExe=NULL;
  59.  
  60. static char ButtonExe1[256];
  61.  
  62. struct Hook SigHook = {
  63.     0
  64. };
  65. extern struct Catalog *Catalog=NULL;
  66.  
  67. #define CATCOMP_BLOCK
  68. #define CATCOMP_NUMBERS
  69. #include "Rmessages.h"
  70.  
  71. ULONG __saveds __asm MyButtons(register __a0 struct Hook *hook,
  72.                                         register __a2 struct MPGuiHandle *gh,
  73.                                         register __a1 ULONG ButtonNo) {
  74.     LONG error;
  75.     sprintf(ButtonExe1,ButtonExe,gh,ButtonNo);
  76.     error = SystemTags(ButtonExe1,
  77.                         SYS_Input,    NULL,
  78.                         SYS_Output,    NULL,
  79.                         NP_Name,        "RunMPGui SubTask",
  80.                         TAG_END);
  81.     if (-1 == error) {
  82.         return 0;
  83.     }
  84.     if (error) {
  85.         return 0;
  86.     }
  87.     return 1;
  88. }
  89.  
  90. ULONG __saveds __asm MySig(register __a0 struct Hook *hook,
  91.                                   register __a2 ULONG signal,
  92.                                   register __a1 ULONG notused) {
  93.     while (agm = GetAmigaGuideMsg(handle)) {
  94.         ReplyAmigaGuideMsg(agm);
  95.     }
  96.     return 1;
  97. }
  98.  
  99. ULONG __saveds __asm Help(register __a0 struct Hook *hook,
  100.                                   register __a2 char *name,
  101.                                   register __a1 APTR notused) {
  102.     char buffer[256];
  103.     while (agm = GetAmigaGuideMsg(handle)) {
  104.         ReplyAmigaGuideMsg(agm);
  105.     }
  106.     sprintf(buffer,"LINK %s",name);
  107.     SendAmigaGuideCmdA(handle,buffer,NULL);
  108.     while (agm = GetAmigaGuideMsg(handle)) {
  109.         ReplyAmigaGuideMsg(agm);
  110.     }
  111.     return 1;
  112. }
  113.  
  114. char
  115. *GetMessage(UWORD message) {
  116.     LONG   *l;
  117.     UWORD  *w;
  118.     STRPTR  builtIn;
  119.  
  120.    l = (LONG *)CatCompBlock;
  121.  
  122.     while (*l != message)  {
  123.         w = (UWORD *)((ULONG)l + 4);
  124.         l = (LONG *)((ULONG)l + (ULONG)*w + 6);
  125.     }
  126.     builtIn = (STRPTR)((ULONG)l + 6);
  127.     return(GetCatalogStr(Catalog,message,builtIn));
  128. }
  129.  
  130. /****** MPGui.library/--RunMPGui-- ******************************************
  131. *
  132. * RunMPGui provides a simple Shell interface to MPGui.library
  133. *
  134. * Parameters are:
  135. *
  136. * FROM/A       Input GUI file
  137. * TO/K         Output file
  138. * RELMOUSE/S   Open reqester by the pointer
  139. * PUBSCREEN/K  Specify the public screen to open on
  140. * HELP/K       AmigaGuide file to show help
  141. * CHELP/S      Show help continuously
  142. * NEWLINE/S    Put a new line between each gadget
  143. * PREFS/S      Show Save/Use/Cancel gadgets rather than OK/Cancel
  144. * BUTTONS/K    Command to run when a button is pressed
  145. * NOBUTTONS/S  Do not show OK/Cancel gadgets
  146. * PARAMS/K/M   Parameters to substitute in GUI file
  147. *
  148. * The BUTTON command should be specified as "command %ld %ld"
  149. *
  150. * The command is then passed two numbers - the address of the handle and
  151. * the number of the button.
  152. *
  153. * Return a failure from this command to Cancel the GUI.
  154. *
  155. * The response from MPGui is:
  156. *
  157. *   0 if OK is pressed;
  158. *   5 if Cancel is pressed;
  159. *  10 if there is an error in the GUI file;
  160. *  20 if there is some fatal error.
  161. *
  162. * Version 5 - Refreshes windows when a requester is open
  163. *             Tries amigaguide.library version 34
  164. * Version 5.2 - Localised.
  165. * Version 5.3 - non beta
  166. * Version 5.4 - Opens locale.library(38) to work on OS3.0.
  167. *
  168. *****************************************************************************
  169. *
  170. */
  171.  
  172. int
  173. main(int argc,char **argv) {
  174.     struct MPGuiHandle *MPGuiHandle;
  175.     char *res;
  176.     BOOL FromWB = FALSE;
  177.     struct RDArgs *rdargs = NULL;
  178.     long opts[OPT_COUNT] = {
  179.         0
  180.     };
  181.     int resx = RETURN_OK;
  182.     struct EasyStruct es = {
  183.         sizeof(struct EasyStruct),
  184.         0,
  185.         "RunMPGui",
  186.         NULL,
  187.         NULL
  188.     };
  189.     BPTR fh;
  190.     struct Hook HelpHook = {
  191.         0
  192.     };
  193.     struct Hook ButtonHook = {
  194.         0
  195.     };
  196.  
  197.     if (!(LocaleBase = OpenLibrary("locale.library",38))) {
  198.         if (FromWB) {
  199.             es.es_TextFormat = "Error Opening locale.library(38)";
  200.             EasyRequestArgs(NULL,&es,NULL,NULL);
  201.         }
  202.         else {
  203.             Printf("Error Opening locale.library(38)");
  204.             Printf((char *)"\n");
  205.         }
  206.         return RETURN_FAIL;
  207.     }
  208.     Catalog = OpenCatalog(NULL,
  209.                                   "mp/runmpgui.catalog",
  210.                                   TAG_END);
  211.     es.es_GadgetFormat = GetMessage(MSG_OK);
  212.     if (argc == 0) {
  213.         argc = _WBArgc;
  214.         argv = _WBArgv;
  215.         FromWB = TRUE;
  216.         if (argc < 2) {
  217.             es.es_TextFormat = GetMessage(MSG_DOUBLE);
  218.             EasyRequestArgs(NULL,&es,NULL,NULL);
  219.             CloseCatalog(Catalog);
  220.             CloseLibrary(LocaleBase);
  221.             return RETURN_WARN;
  222.         }
  223.     }
  224.     else {
  225.         if (!(rdargs = ReadArgs((char *)TEMPLATE, opts, NULL))) {
  226.             PrintFault(IoErr(), NULL);
  227.             CloseCatalog(Catalog);
  228.             CloseLibrary(LocaleBase);
  229.             return RETURN_ERROR;
  230.         }
  231.     }
  232.     AmigaGuideBase = OpenLibrary("amigaguide.library",34);
  233.     if (MPGuiBase = OpenLibrary("MPGui.library",5)) {
  234.         if (opts[OPT_HELP]) {
  235.             if (AmigaGuideBase) {
  236.                 nag.nag_BaseName        = "RunMPGui";
  237.                 nag.nag_Name            = (char *)opts[OPT_HELP];
  238.                 nag.nag_ClientPort    = "RunMPGui_HELP";
  239.                 nag.nag_Flags            = HTF_NOACTIVATE;
  240.                 nag.nag_PubScreen     = (char *)opts[OPT_SCREEN];
  241.                 handle = OpenAmigaGuideAsync(&nag, TAG_END);
  242.             }
  243.         }
  244.         if (handle) {
  245.             ASig = AmigaGuideSignal(handle);
  246.             HelpHook.h_Entry = (HOOKFUNC)Help;
  247.             while (agm = GetAmigaGuideMsg(handle)) {
  248.                 ReplyAmigaGuideMsg(agm);
  249.             }
  250.         }
  251.         ButtonHook.h_Entry = (HOOKFUNC)MyButtons;
  252.         ButtonExe = (char *)opts[OPT_BUTTONS];
  253.         SigHook.h_Entry = (HOOKFUNC)MySig;
  254.         if (MPGuiHandle = AllocMPGuiHandle(MPG_RELMOUSE, opts[OPT_MOUSE],
  255.                                                     MPG_PUBSCREENNAME,opts[OPT_SCREEN],
  256.                                                     MPG_HELP,handle ? (ULONG)&HelpHook : NULL,
  257.                                                     MPG_CHELP,opts[OPT_CHELP],
  258.                                                     MPG_NEWLINE,opts[OPT_NEWLINE],
  259.                                                     MPG_PREFS,opts[OPT_PREFS],
  260.                                                     MPG_PARAMS, opts[OPT_PARAMS],
  261.                                                     MPG_SIGNALS,    ASig,
  262.                                                     MPG_SIGNALHOOK,(ULONG)&SigHook,
  263.                                                     MPG_BUTTONHOOK, opts[OPT_BUTTONS] ? (ULONG)&ButtonHook : NULL,
  264.                                                     MPG_NOBUTTONS, opts[OPT_NOBUTTONS] ? TRUE : FALSE,
  265.                                                     TAG_END)) {
  266.             if (FromWB) {
  267.                 res = SyncMPGuiRequest(argv[1],MPGuiHandle);
  268.             }
  269.             else {
  270.                 res = SyncMPGuiRequest((char *)opts[OPT_FILE],MPGuiHandle);
  271.             }
  272.             if (res == (char *)-1) {
  273.                 if (FromWB) {
  274.                     es.es_TextFormat = (char *)MPGuiError(MPGuiHandle);
  275.                     EasyRequestArgs(NULL,&es,NULL,NULL);
  276.                 }
  277.                 else {
  278.                     Printf(MPGuiError(MPGuiHandle));
  279.                 }
  280.                 resx = RETURN_ERROR;
  281.             }
  282.             else {
  283.                 if (res) {
  284.                     if (opts[OPT_TO]) {
  285.                         if (fh = Open((char *)opts[OPT_TO],MODE_NEWFILE)) {
  286.                             Write(fh,res,strlen(res));
  287.                             Close(fh);
  288.                         }
  289.                         else {
  290.                             if (FromWB) {
  291.                                 es.es_TextFormat = GetMessage(MSG_OUTPUT);
  292.                                 EasyRequest(NULL,&es,NULL,(char *)opts[OPT_TO]);
  293.                             }
  294.                             else {
  295.                                 Printf(GetMessage(MSG_OUTPUT),(char *)opts[OPT_TO]);
  296.                                 Printf((char *)"\n");
  297.                             }
  298.                             resx = RETURN_FAIL;
  299.                         }
  300.                     }
  301.                     else {
  302.                         if (FromWB) {
  303.                             es.es_TextFormat = res;
  304.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  305.                         }
  306.                         else {
  307.                             Printf((char *)res);
  308.                             Printf((char *)"\n");
  309.                         }
  310.                     }
  311.                 }
  312.                 else {
  313.                     if (!opts[OPT_TO]) {
  314.                         if (FromWB) {
  315.                             es.es_TextFormat = GetMessage(MSG_CANCEL);
  316.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  317.                         }
  318.                         else {
  319.                             Printf(GetMessage(MSG_CANCEL));
  320.                             Printf((char *)"\n");
  321.                         }
  322.                     }
  323.                     resx = RETURN_WARN;
  324.                 }
  325.             }
  326.             FreeMPGuiHandle(MPGuiHandle);
  327.         }
  328.         else {
  329.             if (FromWB) {
  330.                 es.es_TextFormat = GetMessage(MSG_HANDLE);
  331.                 EasyRequestArgs(NULL,&es,NULL,NULL);
  332.             }
  333.             else {
  334.                 Printf(GetMessage(MSG_HANDLE));
  335.                 Printf((char *)"\n");
  336.             }
  337.         }
  338.         if (handle) {
  339.             while (agm = GetAmigaGuideMsg(handle)) {
  340.                 ReplyAmigaGuideMsg(agm);
  341.             }
  342.             CloseAmigaGuide(handle);
  343.         }
  344.         CloseLibrary(MPGuiBase);
  345.     }
  346.     else {
  347.         if (FromWB) {
  348.             es.es_TextFormat = GetMessage(MSG_GUIL);
  349.             EasyRequestArgs(NULL,&es,NULL,NULL);
  350.         }
  351.         else {
  352.             Printf(GetMessage(MSG_GUIL));
  353.             Printf((char *)"\n");
  354.         }
  355.         resx = RETURN_FAIL;
  356.     }
  357.     if (AmigaGuideBase) {
  358.         CloseLibrary(AmigaGuideBase);
  359.     }
  360.     if (rdargs) {
  361.         FreeArgs(rdargs);
  362.     }
  363.     CloseCatalog(Catalog);
  364.     CloseLibrary(LocaleBase);
  365.     return resx;
  366. }
  367.